package com.gisuni.sddc.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.json.JSONArray;
import org.json.JSONObject;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import com.gisuni.sddc.service.AdvQueryService;
import com.gisuni.sddc.model.Response;

@IocBean(singleton = false)
@At("/client/service/advQueryController")

public class AdvanceQueryController {
	private Log log = Logs.getLog(AdvanceQueryController.class);
	@Inject
	private AdvQueryService  advQueryService;
	
	@At("/advQuery")
	@GET
	@POST
	@Ok("json")
	public Object landTypeAndKGAdvQuery(@Param("params") String params,HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		JSONObject jsonObj= new JSONObject(params);
		Map<String,Object> map = commonParamHandle(jsonObj);
		if((Boolean) map.get("landTypeFlag")){
			map.putAll(landTypeParamHandle(jsonObj));
		}
		if((Boolean) map.get("KGFlag")){
			map.putAll(KGParamHandle(jsonObj));
		}
		
		if((Boolean) map.get("TGFlag")){
			map.putAll(TGParamHandle(jsonObj));
		}
		try {
			list = advQueryService.advQuery(map);
			res.setResult(list);
			res.setSuccess(true);
		} catch (Exception e) {
			log.error(e.getMessage());
			Map<String,Object> map1=new HashMap<String,Object>();
			map1.put("message", e.getMessage());
			res.setResult(map1);
		}
		return res;
	}
	
	
	private Map<String,Object> commonParamHandle(JSONObject jsonObj){
		Map<String,Object> map=new HashMap<String,Object>();
		//主体
		String mainBody = jsonObj.getString("mainBody");
		map.put("mainBody", mainBody);
		//区域
		String county= jsonObj.getString("county");
		map.put("county", county);
		//面积
		JSONObject area = jsonObj.getJSONObject("area");
		String startValue=area.getString("startValue");
		String endValue = area.getString("endValue");
		map.put("startValue", startValue);
		map.put("endValue", endValue);
		
		//土地类型标识
		Boolean landTypeFlag = jsonObj.getBoolean("landTypeFlag");
		map.put("landTypeFlag", landTypeFlag);
		//控规标识
		Boolean KGFlag = jsonObj.getBoolean("KGFlag");
		map.put("KGFlag", KGFlag);
		//土规标识
		Boolean TGFlag = jsonObj.getBoolean("TGFlag");
		map.put("TGFlag", TGFlag);
		return map;
	}
	private Map<String,Object> landTypeParamHandle(JSONObject jsonObj){
		Map<String,Object> map=new HashMap<String,Object>();
		//土地类型
		String  landType = jsonObj.getString("landType");
		map.put("landType", landType);
		//土地类型表
		String landTypeTable=jsonObj.getString("landTypeTable");
		map.put("landTypeTable", landTypeTable);
		//土地类型查询字段
		String landTypeQueryField=jsonObj.getString("landTypeQueryField");
		map.put("landTypeQueryField", landTypeQueryField);
		
		//土地类型面积字段
		String landTypeAreaField=jsonObj.getString("landTypeAreaField");
		map.put("landTypeAreaField", landTypeAreaField);
		
		return map;
	}
	
	private Map<String,Object> KGParamHandle(JSONObject jsonObj){
		Map<String,Object> map=new HashMap<String,Object>();
		//控规
		JSONArray regulatePlan=jsonObj.getJSONArray("KG");
		map.put("KG", regulatePlan);
		//控规表
		String KGTable=jsonObj.getString("KGTable");
		map.put("KGTable", KGTable);
		//控规查询字段
		String KGQueryField=jsonObj.getString("KGQueryField");
		map.put("KGQueryField", KGQueryField);
		return map;
	}
	
	private Map<String,Object> TGParamHandle(JSONObject jsonObj){
		Map<String,Object> map=new HashMap<String,Object>();
		//土规
		JSONArray tgArr=jsonObj.getJSONArray("TG");
		map.put("TG", tgArr.getString(0));
		//土规表
		String TGTable=jsonObj.getString("TGTable");
		map.put("TGTable", TGTable);
		//土规查询字段
		String TGQueryField=jsonObj.getString("TGQueryField");
		map.put("TGQueryField", TGQueryField);
		return map;
	}
	
	
}
